﻿/********************************************************************************
    Copyright (C) Binod Nepal, Planet Earth Solutions Pvt. Ltd., Kathmandu.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace MixNP.BusinessLayer
{
    public static class Items
    {
        public static bool Hit(double itemId)
        {
            return MixNP.DatabaseLayer.Items.Hit(itemId);
        }

        public static double GetRandomItemId(double itemId)
        {
            return MixNP.DatabaseLayer.Items.GetRandomItemId(itemId);
        }

        public static bool SaveItemImage(double itemId, string filePath)
        {
            return MixNP.DatabaseLayer.Items.SaveItemImage(itemId, filePath);
        }

        public static bool DeleteItemImage(double imageId)
        {
            return MixNP.DatabaseLayer.Items.DeleteItemImage(imageId);
        }

        public static bool DeleteItemImage(string fileName)
        {
            return MixNP.DatabaseLayer.Items.DeleteItemImage(fileName);
        }

        public static double SaveItem(int categoryId, string emailAddress, string name, string title, decimal price, short priceType, string details, List<KeyValuePair<string, string>> customFields, List<KeyValuePair<string, string>> otherFields)
        {
            return MixNP.DatabaseLayer.Items.SaveItem(categoryId, emailAddress, name, title, price, priceType, details, customFields, otherFields);
        }

        public static bool UpdateItem(double itemId, int categoryId, string title, decimal price, short priceType, string details, List<KeyValuePair<string, string>> customFields, List<KeyValuePair<string, string>> otherFields)
        {
            return MixNP.DatabaseLayer.Items.UpdateItem(itemId, categoryId, title, price, priceType, details, customFields, otherFields);
        }

        public static string GetTitle(double itemId)
        {
            return MixNP.DatabaseLayer.Items.GetTitle(itemId);
        }

        public static string GetFirstImage(double itemId)
        {
            return MixNP.DatabaseLayer.Items.GetFirstImage(itemId);
        }

        public static string GetRandomImage(double itemId)
        {
            return MixNP.DatabaseLayer.Items.GetRandomImage(itemId);
        }

        public static string GetAuthorEmail(double itemId)
        {
            return MixNP.DatabaseLayer.Items.GetAuthorEmail(itemId);
        }

        public static bool IsParentCategory(double itemId, int categoryId)
        {
            return MixNP.DatabaseLayer.Items.IsParentCategory(itemId, categoryId);
        }

        public static DataTable GetItem(double itemId)
        {
            return MixNP.DatabaseLayer.Items.GetItem(itemId);
        }

        public static DataTable GetLatestItems()
        {
            return MixNP.DatabaseLayer.Items.GetLatestItems();
        }

        public static DataTable GetPopularItems()
        {
            return MixNP.DatabaseLayer.Items.GetPopularItems();
        }
        
        public static DataTable GetItemAliasView(double itemId)
        {
            return MixNP.DatabaseLayer.Items.GetItemAliasView(itemId);
        }

        public static DataTable GetCustomFields(double itemId)
        {
            return MixNP.DatabaseLayer.Items.GetCustomFields(itemId);
        }

        public static DataTable GetOtherFields(double itemId)
        {
            return MixNP.DatabaseLayer.Items.GetOtherFields(itemId);
        }

        public static DataTable GetImages(double itemId)
        {
            return MixNP.DatabaseLayer.Items.GetImages(itemId);
        }

        public static string GetParentCategoryAlias(double itemId)
        {
            return MixNP.DatabaseLayer.Items.GetParentCategoryAlias(itemId);
        }

        public static string GetCategoryAlias(double itemId)
        {
            return MixNP.DatabaseLayer.Items.GetCategoryAlias(itemId);
        }

        public static string GetItemUrl(double itemId, System.Web.UI.Page page)
        {
            using (DataTable table = GetItemAliasView(itemId))
            {
                if (table.Rows.Count.Equals(1))
                {
                    System.Data.DataRow row = table.Rows[0];
                    string alias = row["ItemAlias"].ToString();
                    string categoryAlias = row["CategoryAlias"].ToString();
                    string parentCategoryAlias = row["ParentCategoryAlias"].ToString();
                    return "http://" + page.Request.Url.Authority + page.ResolveUrl("~/view/" + parentCategoryAlias + "/" + categoryAlias + "/" + itemId.ToString() + "/" + alias + ".mix");
                }
            }

            return string.Empty;
        }

        public static bool AddToWatchlist(string emailAddress, double itemId)
        {
            return MixNP.DatabaseLayer.Items.AddToWatchlist(emailAddress, itemId);
        }

        public static bool RemoveFromWatchlist(string emailAddress, double itemId)
        {
            return MixNP.DatabaseLayer.Items.RemoveFromWatchlist(emailAddress, itemId);
        }

        public static bool AddToWishlist(string emailAddress, double itemId)
        {
            return MixNP.DatabaseLayer.Items.AddToWishlist(emailAddress, itemId);
        }

        public static bool RemoveFromWishlist(string emailAddress, double itemId)
        {
            return MixNP.DatabaseLayer.Items.RemoveFromWishlist(emailAddress, itemId);
        }

        public static bool Renew(double itemId)
        {
            return MixNP.DatabaseLayer.Items.Renew(itemId);
        }

        public static bool Withdraw(double itemId)
        {
            return MixNP.DatabaseLayer.Items.Withdraw(itemId);
        }

        public static bool FlagSold(double itemId)
        {
            return MixNP.DatabaseLayer.Items.FlagSold(itemId);
        }

    }
}
